<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ArticleCategory extends Model
{
    use DataTables;

    protected $table = 'article_category';

    protected $fillable = ['name','pid'];

    //
    public function article()
    {

        return $this->hasMany(Article::class, 'cate_id','id');
    }

    /**
     *
     *   获取所有分类
     */
    public function arrayAllCate()
    {
        //暂时支持二级分类
        $all_first = ArticleCategory::where('pid',0)->get()->toArray();
        $all_p = ArticleCategory::all()->toArray();
        foreach ($all_first as $k_f => $v_f){
            foreach ($all_p as $k => $v){
                if($v['pid'] == $v_f['id']){
                    $all_first[$k_f]['child'][] = $v;
                }
            }
        }
        unset($all_p);
        return $all_first;
    }

}
